<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
<title>Gwack Admin</title>
<link href="style.css" rel="stylesheet" type="text/css" />



<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>

<SCRIPT TYPE="text/javascript">
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=500,height=400,scrollbars=yes');
return false;
}
</SCRIPT>


</head>
<body>
<div id="container">
<?php
include('menu.php');
?>

<div id="content">
	<div id="leftBox">
		<div class="contentBox">
			<div class="contentBoxTop">
			  <h3>GWACK status</h3>
			</div>
			<div class="innerContent">

			<?php

			// Create an array of domain and po names
    		$configfile = '/etc/opt/gwack/gwack.xml';
    		$obj_xml = simplexml_load_file($configfile);
    		foreach ( $obj_xml->server as $server )
    		{
        	if ($server->type == "po") {
        	    $pos[] = $server->name;
        	}
        	if ($server->type == "domain"){
        	    $doms[] = $server->name;
        	}
    		}

			$logrequest=$_GET['logrequest'];
			$URLname=$_GET['name'];
			$xml = simplexml_load_file($configfile);

			$lockfile = '/etc/opt/gwack/gwack.lock';
			$lastrundate = $xml->lastrundate;
			$lastrunstart = $xml->lastrunstart;
			$lastrunend = $xml->lastrunend;
			$Mainlogpath = $xml->logpath;

			if (file_exists($lockfile))
			{
			$running = "1";
			}

			if (substr($Mainlogpath, -1) == "/"){
				$logpath = $Mainlogpath;}
			else {
				$logpath = $Mainlogpath.'/';}

			$loglist = shell_exec("ls -l $logpath | grep -v '^d' | awk '{print $9}'");
			$logfiles = preg_split('/[\r\n]+/', $loglist);



//Function is to create log selector later in the page:

function LogFileSelector($formname = '', $options = array(), $logrequest, $URLname, $logpath)
{
	echo '<select name="'.$formname.'" onchange="MM_jumpMenu(\'parent\',this,0)">';
	foreach ($options as $option => $value) {
		$p = $logpath.$value;	
		echo '<option value="index.php?logrequest='.$p.'&name='.$URLname.'">'.$value.'</option>';
	}

	echo '</select>';
}

?>

			<table class="sTable">
				<tr>
				<thead>
				<th><span strong >&nbsp</strong></th>
				<th>Currently Running</th>
				<th>Last Full Run</th>
				<th>Start Time</th>
				<th>End Time</th>
				<th>View Log</th></span>
				</tr>
				</thead>
				<tr class="oddRow">
				<td class="firstCol">Main</td>
				<td style="text-align:center">
				<?php if ($running == 1){
						echo "<img height='25' src='images/yes.jpg'></style></td>";

						}
						else {
					 echo "<img height='25' src='images/no.jpg'></style></td>";
						}
				echo "<td>$lastrundate</td>";
				echo "<td>$lastrunstart</td>";
				echo "<td>$lastrunend</td>";

				$formname = 'global';
				
				echo "<td><form name='$formname' id='$formname'>";
				$html = LogFileSelector('logrequest', $logfiles, $logrequest, 'global', $logpath);
				echo "</td></tr></table>";
				?>
				</form>

				</div><!-- innercontent end -->				

				<p>
				 <div class="contentBoxTop">
    		          <h3>Domain - PO status</h3>
	            </div>
			
				<div class="innerContent">

			  <table class="sTable">
                   <tr>
                   <thead>
                   <th><span strong >&nbsp</strong></th>
                   <th>Currently Running</th>
                   <th>Restore Path</th>
                   <th>Start Time</th>
                   <th>End Time</th>
                   <th>View Log</th></span>
                    </tr>
                    </thead>
                    <tr class="oddRow">

				<?php
				 foreach ($xml->server as $server)
				{
					$URLname = $server->name;
					$name = $server->name;
					$remotepath = $server->remotepath;
					$type = $server->type;
					$currentfulldate = $server->currentfulldate;
					$currentfullpath = $server->currentfullpath;
					$restorepath = $server->restorepath;
					$starttime = $server->starttime;
					$endtime = $server->endtime;
					$xmllogpath = $xml->logpath;
					$restore = basename($restorepath);

					$agentrunning = "";

					if (substr($xmllogpath, -1) == "/"){
						$logpath = $xmllogpath.$name.'/';}
					else {
						$logpath = $xmllogpath.'/'.$name.'/';}
	
					$lockfile = '/etc/opt/gwack/gwack.lock';

					if (file_exists($lockfile))
					{
						$islock = trim(file_get_contents($lockfile));
						if ($islock == $name)
						{	
							$agentrunning = 1;
						}
					}
					
					$loglist = shell_exec("ls -l $logpath | grep -v '^d' | awk '{print $9}'");
        			$logfiles = preg_split('/[\r\n]+/', $loglist);

					echo "<td class='firstCol'>$name</td>";
                    echo "<td class='secondCol' style='text-align:center;'>";

    	            if ($agentrunning == 1){
    	          		echo "<img height='25' src='images/yes.jpg'></style></td>";
                        }
                        else {
                     echo "<img height='25' src='images/no.jpg'></style></td>";
                        }

					echo "<td>$restore</td>";
    	            echo "<td>$starttime</td>";
    	            echo "<td>$endtime</td>";
    	            echo "<td><form name='$name' id='name'>";
					$html = LogFileSelector('logrequest', $logfiles, $logrequest, $name, $logpath);
    				echo "</td></tr>";
					echo "</form>";	            
				}

				?>
				</table>

			</div><!-- end innerContent -->
		</div><!-- end contentBox -->


		<label for="logView"></label>
		<textarea style="margin-left:7px" name="logView" id="logView" cols="78" rows="10"><?php $LogFile = shell_exec('cat '.$logrequest.''); echo $LogFile; ?></textarea>
	</div><!-- end leftBox -->


	<div id="sidebar" class="rightBoxes">
  		<div class="rightBoxesTop"><h3>Dom - PO Restore</h3></div>
  		<div class="rightContent">
		<ul id="sidebarLinks">



 <?php
            foreach ($doms as $dom) {
                echo "<li><a href='recovery.php?name=".$dom."' target='_self' title='".$dom."'>".$dom." Domain Recovery</a></li>";
            }
             foreach ($pos as $po) {
                echo "<li><a href='recovery.php?name=".$po."' target='_self' title='".$po."'>".$po." Post Office Recovery</a></li>";
            }

?>

</ul>
  </div>
</div><!-- end of #sidebar -->




</div><!-- end content -->


<br class="clearFix" />
</div><!-- end of #container -->



<?php include('footer.html'); ?>
</body>
</html>
